﻿@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Index.cshtml";
}
<style>

    .layui-table-tool > .layui-table-tool-temp {
        padding-right: 0px;
    }
</style>
<script>
    layui.use(['jquery', 'form', 'table', 'common', 'commonTable', 'optimizeSelectOption'], function () {
        var $ = layui.jquery,
            form = layui.form,
            commonTable = layui.commonTable,
            table = layui.table,
            common = layui.common;
        //行操作权限控制
        var toolbar = "#toolbarDemo";
        if (!common.authorizeButtonOne("NF-submit")) {
            toolbar="";
        }
        common.authorizeButtonOne("NF-add");
        common.authorizeButtonOne("NF-delete");
        var itemId;
        commonTable.rendertable({
            elem: '#currentTableId',
            id: 'currentTableId',
            url: '/MaterialManage/Material/GetGridJson',
            search: false,
            defaultToolbar: [],
            toolbar: '',//工具栏
            height: 'full-130',
            filter: {
                clearFilter: false,
                //数据量大，data就不需要了
                items: ['column', 'condition', 'editCondition'],
                bottom : false
            },
            cols: [[
                {
                    field: 'F_MaterialType', title: '料号分类', width: 105, sort: true, filter: true,
                    templet: function (d) {
                        return top.clients.dataItems["Mes_MaterialType"][d.F_MaterialType] == null ? "" : top.clients.dataItems["Mes_MaterialType"][d.F_MaterialType];
                    }
                },
                { field: 'F_MaterialCode', title: '料号编号', width: 150, sort: true, filter: true },
                { field: 'F_MaterialName', title: '料号名称', minWidth: 200, sort: true, filter: true },
                { field: 'F_MaterialDescription', title: '料号描述', width: 120, hide: true, filter: true },
                { field: 'F_MaterialUnit', title: '料号单位', width: 120, hide: true, filter: true },
                { field: 'F_MaterialSize', title: '料号规格', width: 120, hide: true, filter: true },

            ]]
        });
        commonTable.rendertable({
            elem: '#currentTable',
            id: 'currentTable',
            defaultToolbar: [],
            data: [],
            toolbar: toolbar,//工具栏
            search: false,
            limit: 9999,//每页数据 默认
            page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                layout: ['count'] //自定义分页布局
                , first: false //不显示首页
                , last: false //不显示尾页
            },
            height: 'full-220',
            cols: [[
                { type: "checkbox", width: 50 },
                {
                    field: 'F_MaterialType', title: '料号分类', width: 120, sort: true, filter: true,
                    templet: function (d) {
                        return top.clients.dataItems["Mes_MaterialType"][d.F_MaterialType] == null ? "" : top.clients.dataItems["Mes_MaterialType"][d.F_MaterialType];
                    }
                },
                { field: 'F_MaterialCode', title: '料号编号', width: 170, sort: true, filter: true },
                { field: 'F_MaterialName', title: '料号名称', minWidth: 200, sort: true, filter: true },
                { field: 'F_MaterialDescription', title: '料号描述', width: 120, hide: true, filter: true },
                { field: 'F_MaterialUnit', title: '料号单位', width: 120, hide: true, filter: true },
                { field: 'F_MaterialSize', title: '料号规格', width: 120, hide: true, filter: true },
                { field: 'F_Num', title: '数量', width: 120, sort: true, filter: true, edit: 'text' },
                { field: 'F_Description', title: '备注', width: 120, sort: true, filter: true, edit: 'text' },
            ]]
        });
        form.render();
        //加载数据
        wcLoading.close();
        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            //执行搜索重载
            commonTable.reloadtable({
                elem: 'currentTableId',
                curr: 1,
                where: { keyword: data.field.txt_keyword }
            });
            itemId = null;
            item = null;
            return false;
        });
        // 监听搜索操作
        form.on('submit(temp-search-btn)', function (data) {
            common.ajax({
                url: '/MaterialManage/MaterialBom/GetFormJson',
                dataType: 'json',
                data: { itemId: $('#F_ProcessId').val(),keyValue:itemId },
                async: false,
                success: function (data) {
                    if (data.listData) {
                        var datas = JSON.parse(data.listData);
                        table.reload('currentTable', {
                            data: datas
                        });
                    }
                }
            });
            return false;
        });

        table.on('row(currentTableFilter)', function (obj) {
            obj.tr.addClass("layui-table-click").siblings().removeClass("layui-table-click");
            itemId = obj.data.F_Id;
            $("#F_ProcessId").empty();
            $("#F_ProcessId").bindSelect({
                url: "/ProcessManage/ProcessFlow/GetListJsonById?keyValue="+itemId,
                id: "F_Id",
                text: "F_ProcessFlowName"
            });
            form.render();
            common.ajax({
                url: '/MaterialManage/MaterialBom/GetFormJson',
                dataType: 'json',
                data: { itemId: $('#F_ProcessId').val(),keyValue:itemId },
                async: false,
                success: function (data) {
                    if (data.listData) {
                        var datas = JSON.parse(data.listData);
                        table.reload('currentTable', {
                            data: datas
                        });

                    }
                }
            });
        })
        //行点击事件监听，控制按钮显示
        var moreList = ["NF-delete"];//选择多条显示
        commonTable.tableRowClick("checkbox", "currentTable", "currentTable", null, moreList);

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTable)', function (obj) {
            var data = table.checkStatus('currentTable').data;
            if (obj.event === 'add') {  // 监听操作
                if (!itemId) {
                    return false;
                }
                common.modalOpen({
                    title: "添加绑定",
                    url: "/MaterialManage/BomInfo/AddForm?itemId=" + itemId,
                    width: "400px",
                    height: "400px",
                });
            }
            else if (obj.event === 'delete') {
                if (data.length==0) {
                    common.modalMsg("未选中数据", "warning");
                    return false;
                }
                common.modalConfirm("注：您确定要删除该项数据吗？", function (r) {
                    if (r) {
                        var index = parent.layer.load(0, {
                            shade: [0.5, '#000'], //0.1透明度的背景
                        });
                        try {
                            var cachedata = table.cache.currentTable;
                            for (var j = 0; j < data.length; j++) {
                                for (var i = 0; i < cachedata.length; i++) {
                                    if (data[j].F_Id == cachedata[i].F_Id) {
                                        cachedata.splice(i, 1);
                                        break;
                                    }
                                }
                            }

                            table.reload('currentTable', {
                                data: cachedata
                            });
                            entity = null;
                            common.modalMsg("操作成功", "success");
                        } catch (e) {
                            alert(e);
                        }
                        parent.layer.close(index);
                    }
                    else {
                        return false;
                    }
                });
            }
            return false;
        });
        // 监听搜索操作
        form.on('submit(temp-save-btn)', function (data) {
            if (!itemId) {
                common.modalMsg("未选择主物料", "worning");
                return false;
            }
            if (!$('#F_ProcessId').val()) {
                common.modalMsg("未选择工序", "warning");
                return false;
            }
            var cachedata = table.cache.currentTable;
            var postData = data.field;
            postData["F_MaterialId"] = itemId;
            if (cachedata.length != 0) {
                postData.listData = JSON.stringify(cachedata);
            }
            common.submitForm({
                url: '/MaterialManage/BomInfo/SubmitForm',
                param: postData,
                close:false,
                success: function () {
                }
            })
            return false;
        });
                // 监听搜索操作
        form.on('submit(temp-add-btn)', function (data) {
            if (!itemId) {
                common.modalMsg("未选择主物料", "worning");
                return false;
            }
            common.modalOpen({
                title: "添加工序及构成",
                url: "/MaterialManage/BomInfo/Form?itemId=" + itemId,
                width: "650px",
                height: "600px",
            });
            return false;
        });
        form.on('submit(temp-delete-btn)', function (data) {
            if (!itemId) {
                common.modalMsg("未选择主物料", "worning");
                return false;
            }
            if (!$('#F_ProcessId').val()) {
                common.modalMsg("未选择工序", "warning");
                return false;
            }
            common.deleteForm({
                url: "/MaterialManage/BomInfo/DeleteForm",
                param: { itemId:itemId,keyValue: $('#F_ProcessId').val() },
                success: function () {
                    common.reload('data-search-btn');
                }
            });
            return false;
        });
    });

</script>
<div class="layui-fluid" style="padding:0 0px">
    <div class="layui-row layui-col-space5">
        <div class="layui-col-md5 layui-col-xs5">
            <div class="layui-card" style="padding: 5px;">
                <fieldset class="table-search-fieldset" style="border:1px solid #e6e6e6">
                    <legend>生成物料</legend>
                    <form class="layui-form layui-form-pane" action="">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <input type="text" id="txt_keyword" name="txt_keyword" autocomplete="off" class="layui-input" placeholder="">
                            </div>
                            <div class="layui-inline">
                                <button type="submit" class="layui-btn layui-btn-primary" lay-submit lay-filter="data-search-btn"><i class="layui-icon">&#xe615;</i> 搜 索</button>
                            </div>
                        </div>
                    </form>
                </fieldset>
                <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
            </div>
        </div>
        <div class="layui-col-md7 layui-col-xs7">
            <div class="layui-card" style="padding: 5px;">
                <fieldset class="table-search-fieldset" style="border:1px solid #e6e6e6">
                    <legend>工序及构成</legend>
                    <form class="layui-form layui-form-pane" action="">
                        <div class="layui-form-item">
                            <label class="layui-form-label required">选择工序</label>
                            <div class="layui-inline">
                                <select id="F_ProcessId" name="F_ProcessId" lay-search>
                                </select>
                            </div>
                            <div class="layui-inline">
                                <button type="submit" class="layui-btn layui-btn-primary" lay-submit lay-filter="temp-search-btn"><i class="layui-icon">&#xe615;</i> 刷 新</button>
                            </div>
                            <div class="layui-inline">
                                <button type="submit" id="NF-add" class="layui-btn" lay-submit lay-filter="temp-add-btn"><i class="layui-icon">&#xe654;</i>新增工序</button>
                            </div>
                            <div class="layui-inline">
                                <button type="submit" id="NF-delete" class="layui-btn layui-btn-danger " lay-submit lay-filter="temp-delete-btn"><i class="layui-icon">&#xe640;</i>删除工序</button>
                            </div>
                        </div>
                    </form>
                </fieldset>
                <script type="text/html" id="toolbarDemo">
                    <div class="layui-btn-container" id="toolbar">
                        <button id="NF-add" class="layui-btn layui-btn-sm data-add-btn" lay-event="add"><i class="layui-icon">&#xe654;</i>新建</button>
                        <button id="NF-delete" name="NF-delete" class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn layui-hide" lay-event="delete"> <i class="layui-icon">&#xe640;</i>删除</button>
                    </div>
                </script>
                <table class="layui-hide" id="currentTable" lay-filter="currentTable"></table>
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item" style="text-align:center">
                        <div class="layui-inline layui-hide">
                            <button type="submit" id="NF-submit" class="layui-btn layui-btn-warm" lay-submit lay-filter="temp-save-btn"><i class="layui-icon">&#xe605;</i> 保 存</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
